home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0011_Drag a form.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-22  |  1.8 KB  |  86 lines

  1.  
  2. {
  3. Q: How can I make a form move by clicking and dragging in the client area
  4.    instead of on the caption bar?
  5.  
  6. A: The easiest way to do this is to "fool" Windows into thinking that
  7.    you're actually clicking on the caption bar of a form.  Do this by
  8.    handling the wm_NCHitTest windows message as shown in the sample unit
  9.    below.
  10.  
  11.    TIP: If you want a captioness borderless window similar to a floating
  12.    toolbar, set the Form's Caption to an empty string, disable all of the
  13.  
  14.    BorderIcons, and set the BorderStyle to bsNone.
  15. }
  16.  
  17. unit Dragmain;
  18.  
  19. interface
  20.  
  21. uses
  22.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  23.   Forms, Dialogs, StdCtrls;
  24.  
  25. type
  26.   TForm1 = class(TForm)
  27.     Button1: TButton;
  28.     procedure Button1Click(Sender: TObject);
  29.   private
  30.     procedure WMNCHitTest(var M: TWMNCHitTest); message wm_NCHitTest;
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.DFM}
  39.  
  40. procedure TForm1.WMNCHitTest(var M: TWMNCHitTest);
  41.  
  42. begin
  43.   inherited;                    { call the inherited message handler }
  44.   if  M.Result = htClient then  { is the click in the client area?   }
  45.     M.Result := htCaption;      { if so, make Windows think it's     }
  46.                                 { on the caption bar.                }
  47. end;
  48.  
  49. procedure TForm1.Button1Click(Sender: TObject);
  50. begin
  51.   Close;
  52. end;
  53.  
  54. end.
  55.  
  56. { The text representation of the .DFM file is below:
  57.  
  58. object Form1: TForm1
  59.   Left = 203
  60.  
  61.   Top = 94
  62.   BorderIcons = []
  63.   BorderStyle = bsNone
  64.   ClientHeight = 273
  65.   ClientWidth = 427
  66.   Font.Color = clWindowText
  67.   Font.Height = -13
  68.   Font.Name = 'System'
  69.   Font.Style = []
  70.   PixelsPerInch = 96
  71.   TextHeight = 16
  72.   object Button1: TButton
  73.     Left = 160
  74.     Top = 104
  75.     Width = 89
  76.     Height = 33
  77.     Caption = 'Close'
  78.     TabOrder = 0
  79.     OnClick = Button1Click
  80.   end
  81. end
  82.  
  83. }
  84.  
  85.  
  86.